<?php
/*
 * class ini untuk mengelola artikel
 * hanya agen yg bisa mengakses
 */
class Artikel extends CI_Controller{
    var $_admin_view = '';

    function  __construct() {
        parent::__construct();
        $this->load->model('model_admin', 'madmin');
        $this->load->model('model_artikel', 'martikel');
        $this->load->helper('url');
        $this->load->library(array('session','table'));
        $this->config->load('artyproperty');
        $this->_admin_view = $this->config->item('admin_view').'wrapper';
    }

    function index(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
        } else {
            redirect(site_url(),'location');
        }

        $data = array('daftar_artikel' => $this->martikel->get_artikel_admin(),
            'page_title' => 'Daftar Artikel',
            'page_content' => 'daftar_artikel',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function form_tambah_artikel() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
        }else {
            redirect(site_url(),'location');
        }

        $data = array('isUpdate'=>FALSE,
            'page_title' => 'tambah artikel',
            'page_content' => 'tambah_artikel',
            'judul'=>'','isi'=>'',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }
    
    function tambah_artikel(){
        //ambil dari method POST
        $reg['judul'] = $this->input->post('judul');
        $reg['isi'] = $this->input->post('isi');
        $reg['user_id'] = $this->session->userdata('user_id');

        //cek validasi masukkan
        if ($this->validation()) {
            $this->db->trans_begin();
            $this->db->insert('artikel', $reg);
            if ($this->db->trans_status() == FALSE) {
                $this->db->trans_rollback();
            } else {
                $this->db->trans_commit();
                redirect('admin/artikel','location');
            }
        } else {
            $data = array('page_content' => 'tambah_artikel',
                            'page_title' => 'Error | tambah artikel',
                            'level_user' => $this->session->userdata('level'),
                            'user' => $this->session->userdata('username'),
                            'isLogin' => TRUE,'login'=>'',
                            'judul'=>'','isi'=>'',
                            'password' => '','isUpdate' => FALSE,
                            'note'=>'');
        }

        $this->load->view($this->_admin_view, $data);  //tampilkan datanya
    }

    function form_update_artikel(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
        }else {
            redirect(site_url(),'location');
        }
        $artikel_id = $this->uri->segment(4);
        $query = $this->martikel->get_blog(array('artikel_id'=>$artikel_id));
        $udata = $query->row();
        $data = array(
            'isUpdate'=>TRUE,
            'page_title' => 'update artikel',
            'page_content' => 'tambah_artikel',
            'artikel_id' => $udata->artikel_id,
            'judul'=>$udata->judul,'isi'=> $udata->isi,
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function update_artikel($artikel_id=''){
        //ambil dari method POST
        $reg['judul'] = $this->input->post('judul');
        $reg['isi'] = $this->input->post('isi');
        $reg['user_id'] = $this->session->userdata('user_id');

        //cek validasi masukkan
        if ($this->validation()) { 
            $this->db->trans_begin();
            $this->db->where('artikel_id', $artikel_id);
            $this->db->update('artikel',$reg);
            if ($this->db->trans_status() == FALSE) {
                $this->db->trans_rollback();
            } else {
                $this->db->trans_commit();
                redirect('admin/artikel','location');
            }
        } else {
            $artikel_id = $this->uri->segment(4);
            $query = $this->mlisting->get_properti(array('artikel_id'=>$artikel_id));
            $udata = $query->row();
            $data = array('page_content' => 'tambah_artikel',
                            'page_title' => 'Error | update artikel',
                            'level_user' => $this->session->userdata('level'),
                            'user' => $this->session->userdata('username'),
                            'isLogin' => TRUE,'login'=>'',
                            'judul'=>$udata->judul,'isi'=>$udata->isi,
                            'password' => '','isUpdate' => FALSE,
                            'note'=>'');
        }

        $this->load->view($this->_admin_view, $data);  //tampilkan datanya
    }

    function hapus_artikel(){
        $artikel_id = $this->uri->segment(4);

        if(!is_null($artikel_id) AND $artikel_id != 0){
            $this->martikel->hapus_artikel(array('artikel_id'=>$artikel_id));
        }
        redirect('admin/artikel','location');
    }

    public function validation($str=''){
        $this->load->library('form_validation');

        $this->valid = $this->form_validation;

        //konfigurasi aturan2 masukkan valid
        $this->valid->set_rules('judul','judul','trim|required|min_length[1]|max_length[20]');
        $this->valid->set_rules('isi','isi','trim|required|min_length[1]');
        //end konfigurasi

        return ($this->form_validation->run()==FALSE) ? FALSE : TRUE;
    }
}
?>
